package com.gongpingjialoan.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.tool.WebViewActivity;
import com.gongpingjialoan.adapter.LoanRecordAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.data.LoanOrderData;
import com.gongpingjialoan.data.OrderRecord;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.GPSUtil;
import com.gongpingjialoan.utility.Utils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 贷款记录
 * Created by huangcg on 2017/6/6.
 */
public class LoanRecordActivity extends BaseActivity implements NetDataJson.OnStringListener {

    private ListView mListView;

    private LinearLayout nodataImageView;

    private NetDataJson mNetDataJson;

    private LoanRecordAdapter mLoanRecordAdapter;

    private NetDataJson addResultNetDataJson;

    private Button loan_button;
    
    private  List<OrderRecord.DataBean> mRecords;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        action = "my_loanRecord";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loan_layout);
        setTitle("贷款记录");
        nodataImageView = (LinearLayout) findViewById(R.id.no_info);
        mListView = (ListView) findViewById(R.id.listview);
        loan_button = (Button) findViewById(R.id.loan_button);
        String city = Utils.getCityName(getApplicationContext());
        if(("苏州".equals(city) || "成都".equals(city))){
            loan_button.setVisibility(View.VISIBLE);
        }else{
            loan_button.setVisibility(View.GONE);
        }
        loan_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOrderId();
            }
        });
        mRecords = new ArrayList<>();
        mLoanRecordAdapter = new LoanRecordAdapter(mRecords, this);
        mListView.setAdapter(mLoanRecordAdapter);
        mNetDataJson = new NetDataJson(this,1);
        mNetDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN  + API.loan_get_order_id);
        mNetDataJson.setUseOtherDomain(true);
        mNetDataJson.request("get");
    }

    private void getOrderId() {
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                Toast.makeText(LoanRecordActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                Gson gson = new Gson();
                LoanOrderData loanOrderData = gson.fromJson(json, LoanOrderData.class);
                String order_id = loanOrderData.getData().getOrder_no();
                String user_id = loanOrderData.getData().getOrder_no();

                gotoLoanPage(order_id,user_id);

            }
        }, 1);
        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.loan_get_order_id);
        netDataJson.setUseOtherDomain(true);
        netDataJson.addParam("source", 2);
        netDataJson.request("post");
    }

    private void gotoLoanPage(String order_id,String user_id) {
        Intent intent = new Intent(this, WebViewActivity.class);
        String url = "http://www.eflashloan.com/eflashapp/index.html#" + order_id;

        intent.putExtra("url", url);
        intent.putExtra("order_id", order_id);
        intent.putExtra("user_id", user_id);
        intent.putExtra("vehicle_value", "");
        intent.putExtra("access_ip", GPSUtil.getLocalIpAddress());
        intent.putExtra("source", 2);
        intent.putExtra("content", "贷款买车");
        intent.putExtra("loadType","post");
        intent.putExtra("city", Utils.getCityName(getApplicationContext()));
        startActivity(intent);
    }


    @Override
    public void onDataJsonError(String errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
            mNetDataJson = null;
        }
        if (addResultNetDataJson != null) {
            addResultNetDataJson.cancelTask();
            addResultNetDataJson = null;
        }

    }

    @Override
    public void onDataJsonUpdate(String json) {
        Gson gson = new Gson();
        OrderRecord orderRecord = gson.fromJson(json, OrderRecord.class);

        List<OrderRecord.DataBean> mRecords = new ArrayList<>();
        if(orderRecord != null &&orderRecord.getData()!= null){
            mRecords = orderRecord.getData();
        }
        if (0 == mRecords.size()) {
            nodataImageView.setVisibility(View.VISIBLE);
        }
        mLoanRecordAdapter.setData(mRecords);
    }

}
